﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EFong.View.Documentation
{
    public class DocumentHelper
    {
        private object test() 
        {
            return GetRefList("SO00926", "SO1011");
        }
    
        public static List<string> GetRefList(string startRef, string endRef) 
        {
            if (startRef == endRef)
                return new List<string>() { startRef };

            if (startRef.Length != endRef.Length)
                fail();

            int differentIndex = 0;
            for (int i = 0; i < startRef.Length; i++) 
            {
                if (startRef[i] != endRef[i]) 
                {
                    differentIndex = i;
                    break;
                }
            }

            string pre = startRef.Substring(0, differentIndex);
            int start = 0;
            int end = 0;
            int length = startRef.Length - differentIndex;
            try 
            {
                start = Convert.ToInt32(startRef.Substring(differentIndex, length));
                end = Convert.ToInt32(endRef.Substring(differentIndex, length));
            }
            catch (Exception) { fail(); }

            if (start > end)
            {
                int x = start;
                start = end;
                end = x;
            }

            List<string> resultList = new List<string>();

            for (int i = start; i <= end; i++) 
            { 
                resultList.Add(string.Format("{0}{1}",pre , buildNumberCode(i , length)));
            }

            return resultList;
        }

        private static string buildNumberCode(int nr, int length)
        {
            string nrString = nr.ToString();
            if (nrString.Length >= length)
                return nrString;
            int oldLength = nrString.Length;
            for (int i = 0; i < length - oldLength; i++)
                nrString = "0" + nrString;

            return nrString;
        }

        private static void fail() 
        {
            throw new Exception("格式不正确");
        }
    }
}
